package com.mfhcd.facepay.base;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Application;
import android.os.Build;
import android.os.Bundle;


import java.util.ArrayList;

/**
 * 导航控制器
 */
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public class NavigationController implements Application.ActivityLifecycleCallbacks {
    private static final ArrayList<Activity> activities = new ArrayList<>();

    private static class SingletonHolder {
        private static final NavigationController INSTANCE = new NavigationController();
    }

    private NavigationController() {
    }

    public static NavigationController getInstance() {
        return SingletonHolder.INSTANCE;
    }

    public void add(Activity activity) {
        activities.add(activity);
    }

    public int size() {
        return activities.size();
    }

    public void pop() {
        activities.get(activities.size() - 1).finish();
        activities.remove(activities.size() - 1);
    }

    public void popToRoot() {
        for (int i = activities.size() - 1; i >= 0; i--) {
            Activity activity = activities.get(i);
            if (activity.isTaskRoot()) {
                break;
            }

            activity.finish();
            activities.remove(i);
        }
    }

    public Activity get(int index) {
        Activity activity = null;
        if (index < activities.size()) {
            activity = activities.get(index);
        }
        return activity;
    }

    public boolean jumpTo(Activity activity) {
        boolean succeed = false;
        if (activities.contains(activity)) {
            int i = activities.indexOf(activity) + 1;
            for (; i < activities.size(); ) {
                activities.get(i).finish();
                activities.remove(i);
            }
            succeed = true;
        }
        return succeed;
    }

    public boolean isContains(Activity activity) {
        return activities.contains(activity);
    }

    public void clear() {
        for (int i = activities.size() - 1; i >= 0; i--) {
            activities.get(i).finish();
            activities.remove(i);
        }
    }

    public void clear(int step) {
        for (int i = activities.size() - 1; i >= activities.size() - step; i--) {
            activities.get(i).finish();
            activities.remove(i);
        }
    }

    /**
     * 回退几次，参数填几
     *
     * @param YouWantBackTimes 退回的步数
     */
    public void backWithTimes(int YouWantBackTimes) {
        if (YouWantBackTimes >= activities.size()) {
            popToRoot();
        } else {
            int backTimes = YouWantBackTimes - 1;
            int count = activities.size() - 1;
            for (int i = count; i >= count - backTimes; i--) {
                activities.get(i).finish();
                activities.remove(i);
            }
        }
    }


    public Activity getCurrentActivity() {
        Activity activity = null;
        if (!activities.isEmpty()) {
            activity = activities.get(activities.size() - 1);
        }
        return activity;
    }

    /**
     * 结束全部的Activity
     */
    public void finishAllActivity() {
        for (int i = 0, size = activities.size(); i < size; i++) {
            if (null != activities.get(i)) {
                activities.get(i).finish();
            }
        }
        activities.clear();
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        activities.add(activity);
    }

    @Override
    public void onActivityStarted(Activity activity) {

    }

    @Override
    public void onActivityResumed(Activity activity) {
        //isManual 用户手动点击检查，非用户点击操作请传false
        //isSilence 是否显示弹窗等交互，[true:没有弹窗和toast] [false:有弹窗或toast]
    }

    @Override
    public void onActivityPaused(Activity activity) {

    }

    @Override
    public void onActivityStopped(Activity activity) {

    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {
        activities.remove(activity);
    }
}
